﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PGMHelper;

namespace FiberVisible
{
    /// <summary>
    /// 纤维组信息
    /// </summary>
    public class FiberGroup : IComparable
    {
        /// <summary>
        /// 材料编号
        /// </summary>
        public int MatID { set; get; }

        /// <summary>
        /// 是否可见
        /// </summary>
        public bool isVisible { set; get; }

        /// <summary>
        /// 面积
        /// </summary>
        public float A { set; get; }

        /// <summary>
        /// 绕2轴转动惯量
        /// </summary>
        public float Inertia2 { set; get; }

        /// <summary>
        /// 2轴几何中心
        /// </summary>
        public float Centroid2 { set; get; }

        /// <summary>
        /// 绕3轴转动惯量
        /// </summary>
        public float Inertia3 { set; get; }

        /// <summary>
        /// 3轴几何中心
        /// </summary>
        public float Centroid3 { set; get; }

        /// <summary>
        /// Sort拓展
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            int result;
            try
            {
                FiberGroup info = obj as FiberGroup;
                //对比混凝土强度
                if (this.Inertia3 > info.Inertia3)
                    result = -1;
                else if (this.Inertia3 == info.Inertia3)
                    result = 0;
                else result = 1;
                return result;
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }

        /// <summary>
        /// 刷新Textbox
        /// </summary>
        /// <param name="ATextbox"></param>
        /// <param name="IYTextbox"></param>
        /// <param name="IZTextbox"></param>
        /// <param name="YCTextbox"></param>
        /// <param name="ZCTextbox"></param>
        public void ToTextBox(TextBox ATextbox, TextBox IYTextbox, TextBox IZTextbox, 
            TextBox YCTextbox, TextBox ZCTextbox)
        {
            ATextbox.Text = this.A.ToString("f0");
            IYTextbox.Text = this.Inertia3.ToString("E2");
            IZTextbox.Text = this.Inertia2.ToString("E2");
            YCTextbox.Text = this.Centroid3.ToString("f0");
            ZCTextbox.Text = this.Centroid2.ToString("f0");
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="matID"> 材料编号 </param>
        /// <param name="fiberList"> 纤维列表 </param>
        public FiberGroup(int matID, List<FiberInfo> fiberList)
        {
            //获得值
            this.MatID = matID;
            this.isVisible = true;
            //遍历目标纤维
            fiberList.Find("MatID", matID).ForEach(fiber =>
            {
                //计算面积
                this.A += fiber.A;
                //计算绕2轴转动惯量
                this.Inertia2 += (float)(Math.Pow(fiber.y, 2) * fiber.A);
                //计算2轴几何中心
                this.Centroid2 += (float)(fiber.z * fiber.A);
                //计算绕3轴转动惯量
                this.Inertia3 += (float)(Math.Pow(fiber.z, 2) * fiber.A);
                //计算3轴几何中心
                this.Centroid3 += (float)(fiber.y * fiber.A);
            });
            //计算2轴几何中心
            this.Centroid2 = this.Centroid2 / this.A;
            //计算3轴几何中心
            this.Centroid3 = this.Centroid3 / this.A;
        }
    }
}
